package com.hj.dapp.business.mainbudget.controller;

import com.hj.dapp.business.category.model.Category;
import com.hj.dapp.business.mainbudget.service.MainBudgetService;
import com.hj.dapp.business.mainbudget.vo.MainBudgetSaveVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2024/1/29
 */
@RestController
@RequestMapping("/mainbudget")
public class MainBudgetController {
    @Autowired
    private MainBudgetService mainBudgetService;

    @PutMapping("/save")
    public void save(@RequestBody MainBudgetSaveVO mainBudgetSaveVO) {
        mainBudgetService.save(mainBudgetSaveVO);
    }

    @GetMapping("/getMainBudget/{customerId}/{engineerProjectId}")
    public MainBudgetSaveVO getMainBudget(@PathVariable("customerId") Long customerId, @PathVariable("engineerProjectId") Long engineerProjectId) {
        return mainBudgetService.getMainBudget(customerId, engineerProjectId);
    }

    @GetMapping("/getBindCategorys/{mainBudgetId}")
    public List<Category> getBindCategorys(@PathVariable("mainBudgetId") Long mainBudgetId) {
        return mainBudgetService.getBindCategorys(mainBudgetId);
    }

    @GetMapping("/exportMainBudget/{customerId}/{engineerProjectId}")
    public void exportMainBudget(HttpServletResponse response, @PathVariable("customerId") Long customerId, @PathVariable("engineerProjectId") Long engineerProjectId) {


        try {
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + URLEncoder.encode("主材代购单", "UTF-8") + ".xlsx");
            response.flushBuffer();
            ServletOutputStream outputStream = response.getOutputStream();
            mainBudgetService.exportMainBudget(customerId, engineerProjectId, outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
